package mi.payment.utils.transaction;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.DecodePackage;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.iso8583.TLV;
import mi.payment.utils.iso8583.TLVUtils;
import mi.payment.utils.secure.TerminalSecure;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 基于 PBOC 借/贷记标准 IC 卡脚本处理结果通知（含小额支付应用）
 *
 * @author tomcat on 2017/2/8.
 */
class ICScriptResultNotificationTask extends TransactionTask<Object, Void, TradeData> {

    ICScriptResultNotificationTask(Context context) {
        super(context, false);
    }

    @Override
    protected void onBegin(Object[] objects) {
        tradeData = (TradeData) params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        // 预组装
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_IC_SCRIPT_RESULT_NOTIFICATION);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {

        Map<String, MsgField> oriFieldMap = DecodePackage.unAssembly(dbBean.getSendBag());
        // 更新预组装的Map
        fieldMap = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        MsgField f022 = fieldMap.get("F022");
        f022.setDataStr(oriFieldMap.get("F022").getDataStr().substring(0, 3));

        MsgField f032 = fieldMap.get("F032");
        f032.setDataStr(dbBean.getAcquiringBankSpotCode());

        MsgField f055 = fieldMap.get("F055");
        String confirmBag = dbBean.getConfirmBag();
        Map<String, TLV> tlvMap = TLVUtils.builderTLVMap(confirmBag);
        // fix #2149 java.lang.NullPointerException
        // java.lang.NullPointerException:Attempt to invoke interface method 'java.lang.Object java.util.Map.get(java.lang.Object)' on a null object reference
        if (tlvMap == null) {
            tlvMap = new HashMap<>();
        }
        // TODO 拼装55域
        StringBuilder f055Sb = new StringBuilder();
        TLV tag9F33 = tlvMap.get("9F33");
        if (tag9F33 != null) {
            f055Sb.append(tag9F33.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F33.getLength()));
            f055Sb.append(tag9F33.getValue());
        }
        TLV tag95 = tlvMap.get("95");
        if (tag95 != null) {
            f055Sb.append(tag95.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag95.getLength()));
            f055Sb.append(tag95.getValue());
        }
        TLV tag9F37 = tlvMap.get("9F37");
        if (tag9F37 != null) {
            f055Sb.append(tag9F37.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F37.getLength()));
            f055Sb.append(tag9F37.getValue());
        }
        TLV tag9F1E = tlvMap.get("9F1E");
        if (tag9F1E != null) {
            f055Sb.append(tag9F1E.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F1E.getLength()));
            f055Sb.append(tag9F1E.getValue());
        }
        TLV tag9F10 = tlvMap.get("9F10");
        if (tag9F10 != null) {
            f055Sb.append(tag9F10.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F10.getLength()));
            f055Sb.append(tag9F10.getValue());
        }
        TLV tag9F26 = tlvMap.get("9F26");
        if (tag9F26 != null) {
            f055Sb.append(tag9F26.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F26.getLength()));
            f055Sb.append(tag9F26.getValue());
        }
        TLV tag9F36 = tlvMap.get("9F36");
        if (tag9F36 != null) {
            f055Sb.append(tag9F36.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F36.getLength()));
            f055Sb.append(tag9F36.getValue());
        }
        TLV tag82 = tlvMap.get("82");
        if (tag82 != null) {
            f055Sb.append(tag82.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag82.getLength()));
            f055Sb.append(tag82.getValue());
        }
        //发卡行脚本结果
        f055Sb.append("DF31");
        f055Sb.append(TLVUtils.TLVValueLengthToHexString(dbBean.getScriptResult().length() / 2));
        f055Sb.append(dbBean.getScriptResult());
        TLV tag9F1A = tlvMap.get("9F1A");
        if (tag9F1A != null) {
            f055Sb.append(tag9F1A.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9F1A.getLength()));
            f055Sb.append(tag9F1A.getValue());
        }
        TLV tag9A = tlvMap.get("9A");
        if (tag9A != null) {
            f055Sb.append(tag9A.getTag());
            f055Sb.append(TLVUtils.TLVValueLengthToHexString(tag9A.getLength()));
            f055Sb.append(tag9A.getValue());
        }
        f055.setDataStr(f055Sb.toString());
        LogUtil.d(TAG, "F55:" + f055Sb.toString());
        MsgField f060 = fieldMap.get("F060");
        MsgField oriF060 = oriFieldMap.get("F060");
        StringBuilder f060Sb = new StringBuilder();
        f060Sb.append("00");
        f060Sb.append(mSystemParamsSettings.getBatchNumStart());
        f060Sb.append("951");
        if (oriF060.getDataStr().length() > 12) {
            f060Sb.append(oriF060.getDataStr().substring(11, 13));
        }
        f060.setDataStr(f060Sb.toString());

        MsgField f061 = fieldMap.get("F061");
        StringBuilder f061Sb = new StringBuilder();
        f061Sb.append(dbBean.getOriginalBathNo());
        f061Sb.append(dbBean.getOriginalPOSNum());
        f061Sb.append(dbBean.getOriginalTransDate());
        f061.setDataStr(f061Sb.toString());

        // 组包，得到组包后的报文
        return EncodePackage.assembly(fieldMap, "0620", TerminalSecure.PROCESSREQUEST_NONE);
    }


}
